<template>
  <!-- 商品管理-商品类型 -->
  <div class="CommodityManagement">
    <el-card class="box-card CommodityManagement_header">
      <el-form
        ref="formInline"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        :header-cell-style="{
          backgroundColor: 'rgb(243, 246, 251)',
          fontWeight: 500,
          padding: '6px 0'
        }"
      >
        <el-form-item label="商品类型搜索：">
          <el-input v-model="className" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            class="btn_search"
            @click="onSubmit"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card CommodityManagement_header">
      <!-- 新建区域 -->
      <el-row>
        <el-col
          :span="24"
        ><div class="grid-content bg-purple-dark">
          <el-button
            type="primary"
            icon="el-icon-circle-plus-outline"
            class="com_newFile"
            @click="openDialog('add')"
          >新建</el-button>
        </div></el-col>
      </el-row>

      <!-- 表格区域 -->
      <div class="com_table">
        <!-- background-color:#f3f6fb -->
        <el-table :data="tableData" style="width: 100%; margin-top: 20px" :header-cell-style="{background:'#f3f6fb'}">
          <el-table-column prop="classId" label="序号" />
          <el-table-column prop="className" label="商品类型名称" width="1400%" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                size="mini"
                type="text"
                style="color: #5f84ff; font-size: 15px"
                @click="openDialog('edit', row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                style="color: #ff5a5a; font-size: 15px"
                @click="del(row.classId)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页区域 -->
      <div v-if="totalPage > 1" class="com_pagination">
        <el-footer>
          <span>共{{ total }}条记录</span>&nbsp;&nbsp;
          <span>第{{ queryObj.pageIndex }}/{{ totalPage }}页</span>
          <el-pagination
            background
            style="float: right"
            :current-page="+queryObj.pageIndex"
            :page-size="+queryObj.pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="prev, next"
            :total="+total"
            @current-change="handleCurrentChange"
          />
        </el-footer>
      </div>
    </el-card>

    <!-- 新增商品类型弹窗 -->
    <el-dialog
      :title="drawerTitle"
      :visible.sync="DialogVisible"
      width="600px"
      @close="closeDialog"
    >
      <el-form
        ref="formInline"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        :rules="rules"
      >
        <el-form-item label="商品类型名称：" prop="className" label-width="150px">
          <el-input
            v-model="formInline.className"
            size="small"
            placeholder="请输入"
            style="width: 350px"
            maxlength="10"
            show-word-limit
            type="text"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footer">
          <el-button
            class="com_importData"
            @click="DialogVisible = false"
          >取 消</el-button>
          <el-button
            type="primary"
            class="com_newFile"
            @click="dialogConfirm"
          >确 定</el-button>

        </div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCommodityList,
  delCommodityList,
  addCommodityList,
  editCommodityList
} from '@/api/goods'
export default {
  name: 'CommodityManagement',
  data () {
    return {
      formInline: {
        className: ''
      },
      classId: '',
      tableData: [],
      DialogVisible: false,
      total: 0,
      queryObj: {
        pageIndex: 1,
        pageSize: 10
      },
      rules: {
        className: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ]
      },
      className: '',
      drawerType: 'add',
      totalCount: 0,
      totalPage: 0,
      type: ''
    }
  },
  computed: {
    drawerTitle () {
      if (this.drawerType === 'add') return '新增商品类型'
      if (this.drawerType === 'edit') return '修改商品类型'
      else {
        return 'add'
      }
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    // 查询
    async onSubmit () {
      const data = await getCommodityList({ className: this.className })
      this.tableData = data.currentPageRecords
    },
    // 分页
    handleCurrentChange (val) {
      this.queryObj.pageIndex = val
      this.dataList()
    },
    // 获取商品类型列表
    async initTable () {
      const data = await getCommodityList(this.queryObj)
      this.tableData = data.currentPageRecords
      this.totalPage = data.totalPage
      this.total = data.total
      this.totalCount = data.totalCount
    },

    // 打开弹窗
    async openDialog (type, row) {
      this.drawerType = type
      if (type !== 'add') {
        this.formInline.className = row.className
        this.classId = row.classId
      }
      this.DialogVisible = true
    },
    // 提交弹窗方法
    async dialogConfirm () {
      try {
        await this.$refs.formInline.validate()
        if (this.drawerType === 'add') {
          this.formInline.className = ''
          await addCommodityList(this.formInline)
        }
        if (this.drawerType === 'edit') {
          // this.formInline.className = ''
          await editCommodityList(this.classId, this.formInline)
        }
        this.DialogVisible = false
        this.closeDialog()
        this.initTable()
        this.$message.success(
          this.drawerType === 'add' ? '添加成功' : '修改成功'
        )
      } catch (error) {
        console.log(error)
      }
    },

    // 删除
    async del (classId) {
      await delCommodityList(classId)
      this.$message.success('删除成功')
      if (this.tableData.length === 1 && this.queryObj.pageIndex > 1) {
        this.queryObj.pageIndex--
      }
      this.initTable()
    },

    // 关闭弹窗方法
    closeDialog () {
      this.$refs.formInline.resetFields()
      this.DialogVisible = false
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog {
  border-radius: 10px !important;
}
::v-deep .el-dialog__body {
  padding: 0;
}
.footer {
  text-align: center !important;
}
::v-deep .el-table {
 td {
  padding: 8px 0;
  border: none;
 }
 &::before {
  height: 0;
 }
}
.CommodityManagement_header {
  margin: 20px;
}
.com_search {
  margin-left: 10px;
}
.com_input {
  width: 200px;
}
.com_label {
  width: 200px;
}
.com_newFile {
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: 0;
}
.com_importData {
  background-color: #fbf4f0 !important;
  color: #655b56 !important;
  border: 0;
}
.btn_search {
  background-color: #5f84ff;
  border: 0;
}
.com_pagination {
  font-size: 16px;
  color: #dbdfe5 !important;
  padding: 32px 16px;
}
.btn_footer {
  float: right;
  margin-left: 10px;
}
</style>
